package com.zkthink.ceres.promotion.strategy;

import com.zkthink.ceres.promotion.dto.PromotionDetailDTO;
import com.zkthink.exception.BizException;

import java.util.List;

/**
 * <p>
 *    叠加优惠
 * </p>
 *
 * @author vivi.huang
 * @since 2020/6/23
 */
public class PlusDiscountStrategy implements PromotionPlanStrategy {

    /**
     * 叠加优惠
     * 例子：
     *      优惠详情：[满500减50]
     *      订单总价1500
     *      优惠算法: 1500 - (floor(1500 / 500) * 50)
     * @param orderAmount
     * @param promotionDetailDTOList
     * @return
     */
    @Override
    public Long calculateAmount(Long orderAmount, List<PromotionDetailDTO> promotionDetailDTOList) {

        if (promotionDetailDTOList.size() > 1) {
            throw new BizException("叠加优惠的详情超出规则范围");
        }


        PromotionDetailDTO promotionDetailDTO = promotionDetailDTOList.get(0);
        Long result = orderAmount -
                (Long.valueOf(String.valueOf(Math.floor(orderAmount / promotionDetailDTO.getFullAmount())))
                        * promotionDetailDTO.getDiscountAmount());
        return result;
    }
}